/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * addCustomer.java
 *
 * Created on Oct 31, 2011, 4:14:02 PM
 */

package giormi;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.JTable;


/**
 *
 * @author Ling
 */
public class addCustomer extends javax.swing.JFrame {

    /** Creates new form addCustomer */
    private F_addcustomer c = new F_addcustomer();
    private F_updatecustomer uc = new F_updatecustomer();
    private MainFrame main;
    boolean update;
    private String SQLStatement;

    private Connection conn;
    private Statement stmt;
    public ResultSet rset;
    protected JTable table;

    public void setTable(JTable table) {
        this.table = table;
    }
    
    public addCustomer(MainFrame mf, boolean add_update, int idcustomer) {
        initComponents();
        main = mf;
        update = add_update;
        this.setLocation(400, 150);
        this.setResizable(false);
        this.setSize(345, 185);
        this.setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE);

        if (update){
            uc.connect();
            uc.getcustomer(idcustomer);
            c_customernameField.setText(uc.getC_name());
        }
    }

    public void setCustomer(){
        uc.setC_name(c_customernameField.getText());
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveorderButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        c_customernameField = new javax.swing.JTextField();
        c_savecustomerButton = new javax.swing.JButton();
        c_cancelcustomerButton = new javax.swing.JButton();

        saveorderButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Button-Close-icon.png"))); // NOI18N
        saveorderButton1.setText("Cancel");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Manage Customer");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setText("Customer Name");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 110, 20));

        c_customernameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                c_customernameFieldActionPerformed(evt);
            }
        });
        getContentPane().add(c_customernameField, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 20, 180, -1));

        c_savecustomerButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Save-icon.png"))); // NOI18N
        c_savecustomerButton.setText("Save");
        c_savecustomerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                c_savecustomerButtonActionPerformed(evt);
            }
        });
        getContentPane().add(c_savecustomerButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 80, 100, -1));

        c_cancelcustomerButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Button-Close-icon.png"))); // NOI18N
        c_cancelcustomerButton.setText("Cancel");
        c_cancelcustomerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                c_cancelcustomerButtonActionPerformed(evt);
            }
        });
        getContentPane().add(c_cancelcustomerButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 110, 100, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void connect(){
        try{
            DBConnection db = new DBConnection();
            conn = DBConnection.get();
            stmt = conn.createStatement();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void disconnect(){
        try{
            stmt.close();
            conn.close();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }
    private boolean checkCustomer(String cname)
    {
        boolean samecname = false;

        connect();
        try {
            SQLStatement = "SELECT * FROM customer WHERE c_name = '" + cname + "'";
            rset = stmt.executeQuery(SQLStatement);
            System.out.println("same cname select execute");
            if (rset.next()) {
                samecname = true;
                return samecname;
            }
            rset.close();
            disconnect();
            } catch (SQLException ex) {
                Logger.getLogger(addProduct.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NullPointerException ex) {
                System.out.println(ex.getMessage());
            }
        return samecname;
    }
    private void c_customernameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_c_customernameFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_c_customernameFieldActionPerformed

    private void c_savecustomerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_c_savecustomerButtonActionPerformed
        // TODO add your handling code here:
        String cname;
        Boolean samecname = false;
        warningFrame wa;
        cname = c_customernameField.getText();
        confirmFrame co;
        this.setEnabled(false);
        
        c.setC_name(c_customernameField.getText());
        //checking
        if(!cname.equals("")){
            samecname = checkCustomer(cname);
            if(samecname){
                wa = new warningFrame("customerexists", this);
                wa.setVisible(true);               
            }
            else {
                if (update){
                    setCustomer();
                    co = new confirmFrame(main, this, (Object) uc , "customer", update);
                }
                else
                    co = new confirmFrame(main, this, (Object) c , "customer", update);
                co.setTable(table);
                co.setVisible(true);

            }
        }
        else{
            wa = new warningFrame("invaliddata", this);
            wa.setVisible(true);
        }
        //c.setC_name(c_customernameField.getText());
        
        //co = new confirmFrame(main, this, (Object) c , "customer");
        //co.setVisible(true);
//        if(){
//            warningFrame wa = new warningFrame("customerexists", this);
//            wa.setVisible(true);
//        }
//        else if (){
//            warningFrame wa2 = new warningFrame("invaliddata", this);
//            wa2.setVisible(true);
//        }
    }//GEN-LAST:event_c_savecustomerButtonActionPerformed

    private void c_cancelcustomerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_c_cancelcustomerButtonActionPerformed
        // TODO add your handling code here:
        main.setEnabled(true);
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_c_cancelcustomerButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton c_cancelcustomerButton;
    private javax.swing.JTextField c_customernameField;
    private javax.swing.JButton c_savecustomerButton;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton saveorderButton1;
    // End of variables declaration//GEN-END:variables

}
